/* 
 * Author: tangchao@jd.com
 * Date: 2013-05-28
 */
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
#include <string.h>
#include <pthread.h>
#include "global.h"

pthread_mutex_t mutex;
int error = 0;

int process(NG_THD *thd)
{
  int orderid;
  int max;
  MYSQL *mysql = NULL, conn;
  MYSQL_RES *res = NULL;
  char *query;
  query = (char *) malloc(0x400);
  if ( NULL == query)
    printf("Out of memory.\n");
  memset(query, 0, 0x400);


#ifdef debug
  iprint();
#endif

  if (1 == thd->id_offset)
  {
    orderid = thd->id_offset;
    max = thd->query_count - 1;
  } else
  {
    orderid = thd->id_offset * thd->query_count;
    max = orderid + thd->query_count - 1;
  }

  mysql = mysql_init(&conn);
  if ( NULL == mysql)
    goto err;
  mysql->reconnect = 1;
  if (!mysql_real_connect(mysql, thd->host, thd->user, thd->pass, thd->db, thd->port, NULL, 0))
  {
    printf("Eror connecting to database: %s,error code:%d\n", mysql_error(mysql), mysql_errno(mysql));
    goto err;
  } else
  {
    for (; orderid < max; orderid++)
    {
      sprintf(query, "select * from shell where id = %d", orderid);
      if (mysql_real_query(mysql, query, strlen(query)))
        printf("%s, %s, %d\n", query, mysql_error(mysql), mysql_errno(mysql));
      else
      {
        res = mysql_store_result(mysql); // store query result
        if (!res)
          continue;
        mysql_free_result( res);
        sprintf(query, "update shell set addr = "
                "'%s', pin = '%s' where id = %d",
                "我是中国人，死是中国魂，要我学英语，根本不可能",
                "我是中国人，死是中国魂，要我学英语，根本不可能", orderid);
        if ( mysql_real_query(mysql, query, strlen(query)))
          printf("%s, %s, %d\n", query, mysql_error(mysql), mysql_errno(mysql));
      }
    }
    mysql_close( mysql);
    mysql = NULL;
    if (query)
      free(query);
    return 0;
  }

err:
  if (query)
    free(query);
  query= NULL;
  return 1;
}

void *test(void *agc)
{
  pthread_mutex_lock(&mutex);
  NG_THD *thd_tmp = (NG_THD *) agc;
  NG_THD thd;
  sprintf(thd.db, "%s", thd_tmp->db);
  sprintf(thd.host, "%s", thd_tmp->host);
  sprintf(thd.pass, "%s", thd_tmp->pass);
  sprintf(thd.user, "%s", thd_tmp->user);
  thd.id_offset = thd_tmp->id_offset;
  thd.query_count = thd_tmp->query_count;
  thd.port = thd_tmp->port;
  pthread_mutex_unlock(&mutex);
#ifdef debug
  iprint();
#endif
  process(&thd);
  pthread_exit(NULL);
  return; /* Compiler none warning. */
}

int main(int argc, char **argv)
{
  int thread_pos = 0;
  pthread_mutex_init( &mutex, NULL);
  pthread_t thread[0xFFF];
  int max_thread= 20;
  NG_THD *thd = NULL;
  thd = (NG_THD *) malloc(sizeof (NG_THD));
  if ( NULL == thd)
  {
    printf("Out of memory.\n");
    return 0;
  }
  memset(thd, 0, sizeof (NG_THD));

  if (cmdline_parse(argc, argv, thd))
    return 0;

  if (thd->thread > 0xFFF)
  {
    printf("Exit: Max thread num is 0xFFF.\n");
    return 0;
  }
  max_thread= thd->thread;
#ifdef debug
  iprint();
#endif
  printf("Create thread start, ");
  for (thread_pos = 0; thread_pos < max_thread; thread_pos++)
  {
    thd->id_offset = thread_pos + 1;
    if (pthread_create(&thread[thread_pos], NULL, test, thd))
      printf("Thread %d create failed\n", thread_pos);
  }
  printf("thread num:%d\n", thread_pos);

  /* The end */
  for ( thread_pos-- ; thread_pos > 0; thread_pos--)
    pthread_join(thread[thread_pos], NULL);


  printf("error is : %d\n", error);

  return 0;
}
